#include "Poll.h"

#include <stddef.h>

CPoll::CPoll()
    :m_iMaxReadySokcketSize(0), m_iReadySokcketSize(0), m_pReadySocket(NULL)
{
}


CPoll::~CPoll()
{
    if (m_pReadySocket != NULL)
    {
        delete[]m_pReadySocket;
        m_iMaxReadySokcketSize = 0;
        m_iReadySokcketSize = 0;
    }
}

bool CPoll::Init(const int iReadySize)
{
    if (m_pReadySocket != NULL)
    {
        delete[]m_pReadySocket;
    }
    m_iReadySokcketSize = 0;
    m_pReadySocket = new SReadySocket[iReadySize];
    if (m_pReadySocket == NULL)
    {
        return false;
    }

    m_iMaxReadySokcketSize = iReadySize;
    return true;
}

const SReadySocket * CPoll::GetReadySocket()
{
    if (m_pReadySocket == NULL || m_iReadySokcketSize <= 0)
    {
        return NULL;
    }
    
    return &m_pReadySocket[m_iReadySokcketSize - 1];
}

void CPoll::PopReadySocket()
{
    if (m_iReadySokcketSize > 0 && m_pReadySocket != NULL)
    {
        --m_iReadySokcketSize;
    }
}

bool CPoll::AddReadySocket(const int iSocket, const ESocketReadyEventType eType)
{
    if (m_iReadySokcketSize >= m_iMaxReadySokcketSize)
    {
        return false;
    }

    m_pReadySocket[m_iReadySokcketSize].iSocket = iSocket;
    m_pReadySocket[m_iReadySokcketSize].eType = eType;

    ++m_iReadySokcketSize;

    return true;
}
